import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:geomancy/models/festival_article_detail_model.dart';
import 'package:geomancy/models/festival_article_list_model.dart';
import 'package:geomancy/models/festival_list_model.dart';
import 'package:geomancy/tools/config.dart';
import 'package:geomancy/tools/http.dart';

class FestivalAPI {
  ///获取24节气列表
  static Future<List<FestivalItemModel>> get24FestivalList(BuildContext context) async {
    try {
      final data = await Fetch.request(Method.GET, '/jieQi/getList', context: context);
      final result = FestivalListModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        return result.data ?? [];
      }
      return [];
    } catch(e) {
      return [];
    }
  }
  ///获取法定节假日列表
  static Future getHolidayList(BuildContext context) async {
    try {
      ///获取下一年
      final year = DateTime.now().year;
      final data = await Fetch.request(Method.GET, '/api/holiday/year/$year',
        baseUrl: LocalConfig.holidayServerAddress,
        context: context
      );
    } catch(e) {
      //...
    }
  }
  ///获取节气相关文章
  static Future<List<Article>> getArticles(BuildContext context, String name) async {
    try {
      final data = await Fetch.request(Method.GET, '/jieQi/article/list', params: {
        "jieQi": name
      }, context: context);
      final result = FestivalArticleListModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        return result.data ?? [];
      }
      return [];
    } catch(e) {
      return [];
    }
  }
  ///获取节气相关文章详情
  static Future<String?> getArticleDetail(BuildContext context, String url) async {
    try {
      final data = await Fetch.request(Method.GET, '/jieQi/article/detail', params: {
        "url": url
      }, context: context);
      final result = FestivalArticleDetailModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        return result.data?.content;
      }
      return null;
    } catch(e) {
      return null;
    }
  }
}